package com.example.activityapplication;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.AlarmClock;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class FirstActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);

        Button btn1 = findViewById(R.id.btn_first);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*Intent显式用法,跳转*/
                /*Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
                intent.putExtra("FirstActivity","Hello SecondActivity!!!!");
                startActivity(intent);*/

                /*Intent隐式用法，要在AndroidManifest.xml里需要跳转到的写<intent-filter> </intent-filter>*/
                /*Intent intent = new Intent("com.example.activityapplication.MY_ACTION");
                intent.addCategory("android.intent.category.DEFAULT");
                startActivity(intent);*/

                /*用Intent打开网页*/
                /*openWebPage("https://www.lofter.com/front/login");*/

                /*用Intent设置闹钟*/
                /*createAlarm("起床",9,30);*/

                /*用Intent拨打电话*/
               /* makeCall("13568740021");*/

                /*用Intentd打开地图*/
                /*showMap(Uri.parse("geo:47.6,-122.3"));*/


                /*处理目标组件返回的数据*/
                /*Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
                startActivityForResult(intent,1);*/

                //Bundle传输复杂数据
                Bundle bundle = new Bundle();
                bundle.putString("name","FirstActivity");
                bundle.putInt("id",121);

                Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
                intent.putExtra("info",bundle);
                startActivity(intent);



            }
        });
    }
    /*处理目标组件返回的数据，在Generate中的Override Method找onActivityResult*/
   /* @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case 1: *//*startActivityForResult中的requestCode值*//*
                if (resultCode == RESULT_OK){
                    String returnedData = data.getStringExtra("data_second");
                    Log.d("FirstActivity",returnedData);
                }
                break;
            default:
                break;
        }
    }*/

    /*用Intent打开网页*/
    public void openWebPage(String url){
        Uri webpage = Uri.parse(url);
        Intent intent = new Intent(Intent.ACTION_VIEW,webpage);
        if(intent.resolveActivity(getPackageManager()) !=null) {
            startActivity(intent);
        }


    }

    /*用Intent设置闹钟*/
    public void createAlarm(String message,int hour,int minutes){
        Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM)
                .putExtra(AlarmClock.EXTRA_MESSAGE,message)
                .putExtra(AlarmClock.EXTRA_HOUR,hour)
                .putExtra(AlarmClock.EXTRA_MINUTES,minutes);
        if (intent.resolveActivity(getPackageManager()) !=null){
            startActivity(intent);
        }
    }

    /*用Intent拨打电话*/
    public void makeCall(String phoneNumber){
        Intent intent = new Intent(Intent.ACTION_DIAL);
        intent.setData(Uri.parse("tel:" + phoneNumber));
        if (intent.resolveActivity(getPackageManager()) !=null){
            startActivity(intent);
        }
    }

    /*用Intentd打开地图*/
    public void showMap(Uri geoLoacation){
        /*showMap(Uri.parse("geo:47.6,-122.3"));*/
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(geoLoacation);
        if (intent.resolveActivity(getPackageManager()) !=null){
            startActivity(intent);
        }
    }
}